package com.xgq.drink.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xgq.drink.entity.OrderProduct;
import com.xgq.drink.mapper.OrderProductMapper;
import com.xgq.drink.service.IOrderProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xgq
 * @since 2025-03-04
 */
@Service
public class OrderProductServiceImpl extends ServiceImpl<OrderProductMapper, OrderProduct> implements IOrderProductService {

    @Autowired
    private OrderProductMapper orderProductMapper;
    /**
     * 保存订单中的商品信息
     * @param orderProducts 订单中的商品列表
     */
    @Override
    public void saveOrderProducts(List<OrderProduct> orderProducts) {
        saveBatch(orderProducts);
    }

    @Override
    public List<Long> getOrderIdsByProductIds(List<Long> productIds) {
        if (productIds == null || productIds.isEmpty()) {
            return List.of();
        }
        
        // 查询包含指定产品的订单产品记录
        QueryWrapper<OrderProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("product_id", productIds);
        List<OrderProduct> orderProducts = list(queryWrapper);
        
        // 提取不重复的订单ID
        return orderProducts.stream()
                .map(OrderProduct::getOrderId)
                .distinct()
                .collect(Collectors.toList());
    }
}
